<% title t("views.moderations.actions.meta.title", title: @moderatable.title) %>

<% cache(release_adjusted_cache_key("mod-styling")) do %>
  <style>
    <%= Rails.application.assets["moderators.css"].to_s.html_safe %>
  </style>
<% end %>

<%= javascript_include_tag "actionsPanel", defer: true %>

<% author_suspended = @moderatable.user.suspended? %>
<% author_username = @moderatable.username %>

<div class="container mod-container">
  <header class="top-header">
    <h1><%= t("views.moderations.actions.heading") %></h1>
    <h2><%= t("views.moderations.actions.subtitle") %></h2>
    <button class="crayons-btn crayons-btn--secondary crayons-btn--icon-rounded close-actions-panel hidden" type="button" title="<%= t("views.moderations.actions.close") %>">
      <%= crayons_icon_tag(:x, title: t("views.moderations.actions.close")) %>
    </button>
  </header>

  <div class="reactions-container">
    <div class="thumb-reactions-container">
      <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "thumbsup") ? "reacted" : "" %>"
              data-reactable-id="<%= @moderatable.id %>"
              data-category="thumbsup"
              data-reactable-type="<%= @moderatable.class.name %>">
        <div class="reaction-button-circle">
          <%= crayons_icon_tag("twemoji/thumb-up", native: true, title: t("views.moderations.actions.thumb_up")) %>
          <%= crayons_icon_tag(:checkmark, class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
        </div>
        <span class="vote-text"><%= t("views.moderations.actions.vote_up") %></span>
      </button>
      <button class="reaction-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "thumbsdown") ? "reacted" : "" %>"
              data-reactable-id="<%= @moderatable.id %>"
              data-category="thumbsdown"
              data-reactable-type="<%= @moderatable.class.name %>">
        <div class="reaction-button-circle">
          <%= crayons_icon_tag("twemoji/thumb-down", native: true, title: t("views.moderations.actions.thumb_down")) %>
          <%= crayons_icon_tag(:checkmark, class: "reaction-checkmark", title: t("views.moderations.actions.checkmark")) %>
        </div>
        <span class="vote-text"><%= t("views.moderations.actions.vote_down") %></span>
      </button>
    </div>
    <button class="reaction-vomit-button <%= Reaction.cached_any_reactions_for?(@moderatable, current_user, "vomit") ? "reacted" : "" %>"
            data-reactable-id="<%= @moderatable.id %>"
            data-category="vomit"
            data-reactable-type="<%= @moderatable.class.name %>">
      <%= crayons_icon_tag("twemoji/flag", native: true, class: "mr-1", title: "Flag") %>
      <span></span>
      <%= crayons_icon_tag(:checkmark, class: "vomit-checkmark", title: t("views.moderations.actions.checkmark")) %>
    </button>
    <% if policy(@moderatable).revoke_publication? %>
      <button
        class="c-btn c-btn--primary c-btn--destructive w-100 my-4 <%= is_mod_center == "true" ? "hidden" : "" %>"
        id="unpublish-article-btn"
        data-article-id="<%= @moderatable.id %>"
        data-author-username="<%= author_username %>"
        data-article-slug="<%= @moderatable.slug %>"
        data-modal-content-selector="#unpublish-post-modal">
        <%= t("views.moderations.actions.unpublish") %>
      </button>
    <% end %>
    <% if policy(@moderatable).toggle_featured_status? %>
      <button
        class="c-btn c-btn--primary w-100"
        id="feature-article-btn"
        data-article-featured="<%= @moderatable.featured %>"
        data-article-id="<%= @moderatable.id %>"
        data-article-author="<%= author_username %>"
        data-article-slug="<%= @moderatable.slug %>">
        <%= @moderatable.featured ? t("views.moderations.actions.unfeature") : t("views.moderations.actions.feature") %>
      </button>
      <div class="additional-subtext-section py-4 pt-2">
        <% recent_featured_count = Article.from_subforem.featured.published.where(published_at: 1.day.ago..).size %>
        <%= t("views.moderations.actions.featured_past_day", count: recent_featured_count) %>
      </div>
    <% end %>
    <% unless is_mod_center %>
      <a href="<%= URL.url %>/community-moderation#using-the-quick-reactions-to-moderate-content" target="_blank" rel="noopener">
        <span class="additional-subtext-section py-3">
          <%= t("views.moderations.actions.how") %>
        </span>
      </a>
    <% end %>
  </div>

  <div class="other-things-container">
    <header class="other-things">
      <h3>
        <%= t("views.moderations.actions.other") %>
      </h3>
    </header>

    <%# Checking if the user should see the "Adjust tags" dropdown %>
    <% if policy(@moderatable).allow_tag_adjustment? %>
      <button id="adjust-tags-button" aria-haspopup="true" aria-expanded="false" aria-controls="adjust-tags-options" class="other-things-btn adjust-tags" type="button" data-other-things-type="adjust-tags" aria-label="<%= t("views.moderations.actions.adjust.aria_label") %>">
        <div class="label-wrapper">
          <div class="icon circle centered-icon">
            <%= crayons_icon_tag("mod-filled", title: t("views.moderations.actions.adjust.icon")) %>
          </div>
          <header>
            <h2><%= t("views.moderations.actions.adjust.heading") %></h2>
            <h3><%= t("views.moderations.actions.adjust.subtitle") %></h3>
          </header>
        </div>
        <div class="toggle-chevron-container">
          <%= crayons_icon_tag("arrow-down-fill", title: t("views.moderations.actions.toggle")) %>
        </div>
      </button>

      <div id="adjust-tags-options" class="adjust-tags-options dropdown-options hidden">
        <% @moderatable.tags.each do |tag| %>
          <% last_added_by_admin = last_adjusted_by_admin?(@moderatable, tag, "addition") %>
          <% current_tag_moderator = @tag_moderator_tags.pluck(:name).include?(tag.name) %>

          <%# The tag is moderatable if the user is a current moderator of that tag and that tag was not added by admin OR the user is an admin %>
          <% moderatable_tag = (current_tag_moderator && !last_added_by_admin) || policy(@moderatable).can_adjust_any_tag? %>

          <button
            id="remove-tag-button-<%= tag.name %>"
            class="adjustable-tag" type="button"
            data-adjustment-type="subtract"
            data-tag-name="<%= tag.name %>"
            data-article-id="<%= @moderatable.id %>"
            title="<%= moderatable_tag ? "" : "Not allowed to adjust this tag" %>">
            <span class="num-sign">#</span><%= tag.name %>
            <% if moderatable_tag %>
              <div id="remove-tag-icon-<%= tag.name %>" class="circle centered-icon remove-icon hidden">
                <%= crayons_icon_tag(:minus, title: t("views.moderations.actions.adjust.remove")) %>
              </div>
            <% end %>
          </button>

          <% if current_tag_moderator && last_added_by_admin %>
            <div class="fs-s px-3 adjustment-restriction">
              <%= t("views.moderations.actions.adjust.cannot_remove_admin_adjusted_tag_html", contact_email: ForemInstance.contact_email) %>
            </div>
          <% end %>

          <% if moderatable_tag %>
            <div id="remove-tag-container-<%= tag.name %>" class="hidden">
              <div id="adjustment-reason-container-<%= tag.name %>" class="reason-container">
                <textarea class="crayons-textfield" placeholder="<%= t("views.moderations.actions.adjust.reason.remove_tag") %>" id="tag-removal-reason-<%= tag.name %>"></textarea>
                <div class="flex gap-3 align-items-stretch">
                  <button class="w-100 c-btn c-btn--primary c-btn--destructive" id="remove-tag-submit-<%= tag.name %>"><%= t("views.moderations.actions.adjust.remove") %></button>
                  <button class="w-100 c-btn" id="cancel-remove-tag-button-<%= tag.name %>" type="button"><%= t("views.moderations.actions.adjust.cancel") %></button>
                </div>
              </div>
            </div>
          <% end %>
        <% end %>
        <% if @moderatable.tag_list.size < Article::MAX_TAG_LIST_SIZE %>
          <% if policy(@moderatable).can_adjust_any_tag? %>
            <hr />
            <button id="add-tag-button" data-testid="add-tag-button" class="add-tag" type="button">
              <%= t("views.moderations.actions.adjust.add_tag.button_text") %>
              <div class="circle centered-icon add-icon">
                <%= crayons_icon_tag(:plus, title: t("views.moderations.actions.adjust.add_tag.icon")) %>
              </div>
            </button>
            <div id="add-tag-container" class="hidden">
              <div id="add-reason-container" class="reason-container">
                <div class="add-tag-header"><%= t("views.moderations.actions.adjust.add_tag.header") %></div>
                <input id="admin-add-tag" class="crayons-textfield mt-3" type="text" autocomplete="off" placeholder="<%= t("views.moderations.actions.adjust.add_placeholder") %>" data-article-id="<%= @moderatable.id %>" data-adjustment-type="plus">
                <textarea id="tag-add-reason" class="crayons-textfield mt-3" placeholder="<%= t("views.moderations.actions.adjust.reason.add_tag") %>"></textarea>
                <div class="flex gap-3">
                  <button class="w-100 c-btn c-btn--primary" disabled="disabled" id="tag-add-submit"><%= t("views.moderations.actions.adjust.add") %></button>
                  <button class="w-100 c-btn" id="cancel-add-tag-button" type="button"><%= t("views.moderations.actions.adjust.cancel") %></button>
                </div>
              </div>
            </div>
          <% else %>
            <% count = 0 %>
            <% @tag_moderator_tags.each do |tag| %>
              <% if !@moderatable.tags.pluck(:name).include?(tag.name) %>
                <% count += 1 %>
              <% end %>
            <% end %>

            <% if count > 0 %>
              <hr />
            <% end %>
            <% @tag_moderator_tags.each do |tag| %>
              <% last_removed_by_admin = last_adjusted_by_admin?(@moderatable, tag, "removal") %>

              <% if !@moderatable.tags.pluck(:name).include?(tag.name) %>

                <% if last_removed_by_admin %>
                  <div class="adjustment-restriction__container px-3 align-left">
                    <div class="adjustment-restriction__container--tag fs-base"><span class="num-sign px-05">#</span><%= tag.name %></div>
                    <div class="fs-s adjustment-restriction__container--text">
                      <%= t("views.moderations.actions.adjust.cannot_add_admin_adjusted_tag_html", contact_email: ForemInstance.contact_email) %>
                    </div>
                  </div>
                <% else %>
                  <button
                    id="add-tag-button-<%= tag.name %>"
                    class="adjustable-tag add-tag" type="button"
                    data-adjustment-type="plus"
                    data-tag-name="<%= tag.name %>"
                    data-article-id="<%= @moderatable.id %>">
                    <span class="num-sign">#</span><%= tag.name %>
                    <div id="add-tag-icon-<%= tag.name %>" class="circle centered-icon add-icon">
                      <%= crayons_icon_tag(:plus, title: t("views.moderations.actions.adjust.add")) %>
                    </div>
                  </button>
                  <div id="add-tag-container-<%= tag.name %>" class="hidden">
                    <div id="add-reason-container-<%= tag.name %>" class="reason-container">
                      <textarea id="tag-add-reason-<%= tag.name %>" class="crayons-textfield mt-3" placeholder="<%= t("views.moderations.actions.adjust.reason.add_tag") %>"></textarea>
                      <div class="flex gap-3">
                        <button class="w-100 c-btn c-btn--primary" id="tag-add-submit-<%= tag.name %>"><%= t("views.moderations.actions.adjust.add") %></button>
                        <button class="w-100 c-btn" id="cancel-add-tag-button-<%= tag.name %>" type="button"><%= t("views.moderations.actions.adjust.cancel") %></button>
                      </div>
                    </div>
                  </div>
                <% end %>
              <% end %>

            <% end %>
          <% end %>
          <hr />
        <% else %>
          <hr />
          <span class="max-tags-text">
            <%= crayons_icon_tag(:info, title: t("views.moderations.actions.adjust.maximum_tags.icon"), width: 18, height: 18) %>
            <span>
              <%= t("views.moderations.actions.adjust.maximum_tags.text") %>
            </span>
          </span>
        <% end %>
        <% if @moderatable.ordered_tag_adjustments.any? %>
          <div id="tag-moderation-history">
            <h1><%= t("views.moderations.actions.adjust.previous") %></h1>
            <% @moderatable.ordered_tag_adjustments.each do |adj| %>
              <div id="tag-adjustment-<%= adj.id %>" class="tag-adjustment">
                <span class="num-sign">#</span>
                <span><%= adj.tag_name %></span>
                <span class="adjustment-type">
                  <%= if adj.adjustment_type == "addition"
                        t("views.moderations.actions.adjust.history.added_by")
                      else
                        t("views.moderations.actions.adjust.history.removed_by")
                      end %>
                </span>
                <span><%= adj.user.name %></span>
                <div class="tag-adjustment-reason">
                  <span><%= adj.reason_for_adjustment %></span>
                </div>
              </div>
            <% end %>
          </div>
        <% end %>
        <a href="<%= URL.url %>/tag-moderation#how-to-remove-or-add-tags" target="_blank" rel="noopener">
          <span class="additional-subtext-section py-3">
            <%= t("views.moderations.actions.how") %>
          </span>
        </a>
      </div>
    <% end %>

    <% @rating_vote = RatingVote.where(article_id: @moderatable.id, user_id: current_user.id).first %>
    <button aria-haspopup="true" aria-expanded="false" aria-controls="set-experience-options" class="other-things-btn set-experience" type="button" data-other-things-type="set-experience" aria-label="<%= t("views.moderations.actions.experience.aria_label") %>">
      <div class="label-wrapper">
        <div class="icon circle centered-icon">
          <%= crayons_icon_tag("book", title: t("views.moderations.actions.experience.icon")) %>
        </div>
        <header>
          <h2><%= t("views.moderations.actions.experience.heading") %></h2>
          <h3><%= t("views.moderations.actions.experience.subtitle") %></h3>
        </header>
      </div>
      <div class="toggle-chevron-container">
        <%= crayons_icon_tag("arrow-down-fill", title: t("views.moderations.actions.toggle")) %>
      </div>
    </button>

    <div id="set-experience-options" class="set-experience-options dropdown-options hidden">
      <% rating_hash = { "Expert" => [10, 5], "Advanced" => [8, 4], "Mid-level" => [5, 3], "Beginner" => [3, 2], "Novice" => [1, 1] } %>
      <% current_rating_value = @rating_vote.rating.round(0) if @rating_vote %>
      <% rating_hash.each do |rating_name, rating_level| %>
      <button
        value="<%= rating_level[0] %>"
        name="rating_vote_<%= rating_level[0] %>"
        id="js__rating__vote__<%= rating_level[0] %>"
        class="level-rating-button <%= " selected" if current_rating_value == rating_level[0] %>"
        data-user-id="<%= current_user.id %>"
        data-article-id="<%= @moderatable.id %>"
        data-group="experience_level">
        <div class="level-rating-content">
          <span class="level-rating-number"><%= rating_level[1] %></span>
          <span class="level-rating-text"><%= t("views.moderations.actions.experience.level.#{rating_name}") %></span>
        </div>
        <%= crayons_icon_tag(:checkmark, title: t("views.moderations.actions.checkmark")) %>
      </button>
      <% end %>
      <a href="<%= URL.url %>/community-moderation#how-to-rate-the-experience-level-of-a-post" target="_blank" rel="noopener">
        <span class="additional-subtext-section py-3">
          <%= t("views.moderations.actions.how") %>
        </span>
      </a>
    </div>

    <button
      aria-haspopup="true"
      aria-expanded="false"
      aria-controls="admin-action-options"
      class="other-things-btn admin-action <%= is_mod_center == "true" ? "hidden" : "" %>"
      type="button"
      data-other-things-type="admin-action"
      aria-label="<%= t("views.moderations.actions.admin.aria_label") %>">
      <div class="label-wrapper">
        <div class="icon circle centered-icon">
          <%= crayons_icon_tag("admin", title: t("views.moderations.actions.admin.icon")) %>
        </div>
        <header>
          <h2><%= t("views.moderations.actions.admin.heading") %></h2>
          <h3><%= t("views.moderations.actions.admin.subtitle", user: author_username) %></h3>
        </header>
      </div>
      <div class="toggle-chevron-container">
        <%= crayons_icon_tag("arrow-down-fill", title: t("views.moderations.actions.toggle")) %>
      </div>
    </button>

    <div id="admin-action-options" class="admin-action-options dropdown-options mt-4 hidden">
      <button
        type="button"
        id="toggle-flag-user-modal"
        class="c-btn <%= @author_flagged ? "" : "c-btn--destructive" %> align-center fs-s"
        data-modal-title="<%= @author_flagged ? "Unflag" : "Flag" %> <%= author_username %>"
        data-modal-size="small"
        data-modal-content-selector="#<%= @author_flagged ? "unflag" : "flag" %>-user-modal-content">
        <% if @author_flagged %>
          <%= t("views.moderations.actions.unflag_user", username: author_username) %>
        <% else %>
          <%= t("views.moderations.actions.flag_user", username: author_username) %>
        <% end %>
      </button>
      <% if policy(@moderatable).can_perform_moderator_actions? %>
        <button
          type="button"
          id="<%= author_suspended ? "unsuspend" : "suspend" %>-user-btn"
          class="js-suspend-unsuspend-flow-btn c-btn <%= author_suspended ? "" : "c-btn--destructive" %> align-center fs-s"
          data-modal-title="<%= t("views.moderations.actions.suspend.#{author_suspended ? 'unsuspend' : 'suspend'}_title", username: author_username) %>"
          data-modal-size="small" data-modal-content-selector="#<%= author_suspended ? "unsuspend" : "suspend" %>-modal-content" data-username="<%= author_username %>">
            <%= t("views.moderations.actions.suspend.#{author_suspended ? 'unsuspend' : 'suspend'}_user", username: author_username) %>
        </button>

        <button
          type="button"
          id="unpublish-all-posts-btn"
          class="c-btn c-btn--destructive align-center fs-s"
          data-modal-title="<%= t("views.moderations.actions.unpublish_all.title") %>"
          data-modal-size="small"
          data-modal-content-selector="#unpublish-all-posts"
          data-username="<%= author_username %>">
          <%= t("views.moderations.actions.unpublish_all.button", username: author_username) %>
        </button>
      <% end %>
    </div>
  </div>
</div>
